/*
 *	Load Monitor
 *
 *	Copyright © 2001 Alexandre Vial, some parts from Bernhard Baehr
 *
 *	Preferences.m - Preferences Controller Class
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#import "Preferences.h"


@implementation Preferences


+ (NSMutableDictionary *)defaultPreferences
{
	return ([[NSMutableDictionary alloc] initWithObjectsAndKeys:
		[NSColor yellowColor], WIRED_COLOR_KEY,
//		[NSColor magentaColor], ACTIVE_COLOR_KEY,
//		[NSColor cyanColor], INACTIVE_COLOR_KEY,
		[NSColor blueColor], FREE_COLOR_KEY,
		[NSColor whiteColor], PAGEIN_COLOR_KEY,
		[NSColor blackColor], PAGEOUT_COLOR_KEY,
		[NSNumber numberWithFloat:0.8], TRANSPARENCY_KEY,
		[NSNumber numberWithInt:20], UPDATE_FREQUENCY_KEY,	/* unit is 1/10 second */
		[NSNumber numberWithInt:1], PAGING_SCALE_MAX_KEY,
		[NSNumber numberWithBool:YES], PAGEIN_ATOP_PAGEOUT_KEY,
		[NSNumber numberWithBool:YES], SHOW_PAGING_RATE_KEY,
		[NSNumber numberWithBool:NO], SHOW_GRAPH_WINDOW_KEY,
		[NSNumber numberWithBool:NO], GRAPH_WINDOW_ON_TOP_KEY,
		[NSNumber numberWithInt:128], GRAPH_WINDOW_SIZE_KEY,
		nil]);
}


- (id)init
{
#define SCANCOLOR(key)	\
	if ((obj = [defaults objectForKey:key])) { \
		sscanf ([obj cString], "%f %f %f", &r, &g, &b); \
		obj = [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0]; \
		[currentSettings setObject:obj forKey:key]; \
	}
#define GETNUMBER(key)	\
	if ((obj = [defaults objectForKey:key])) \
		[currentSettings setObject:obj forKey:key];

	id	obj;
	float	r, g, b;

	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	
	self = [super init];
        currentSettings = [Preferences defaultPreferences];
	SCANCOLOR (WIRED_COLOR_KEY);
//	SCANCOLOR (ACTIVE_COLOR_KEY);
//	SCANCOLOR (INACTIVE_COLOR_KEY);
	SCANCOLOR (FREE_COLOR_KEY);
	SCANCOLOR (PAGEIN_COLOR_KEY);
	SCANCOLOR (PAGEOUT_COLOR_KEY);
	GETNUMBER (TRANSPARENCY_KEY);
	GETNUMBER (UPDATE_FREQUENCY_KEY);
	GETNUMBER (PAGING_SCALE_MAX_KEY);
	GETNUMBER (PAGEIN_ATOP_PAGEOUT_KEY);
	GETNUMBER (SHOW_PAGING_RATE_KEY);
	GETNUMBER (SHOW_GRAPH_WINDOW_KEY);
	GETNUMBER (GRAPH_WINDOW_ON_TOP_KEY);
	GETNUMBER (GRAPH_WINDOW_SIZE_KEY);
	return (self);
}


- (void)showUpdateFrequency:(int)freq
{
	[updateFrequency setStringValue:[NSString stringWithFormat:@"%.1f sec.", freq / 10.0]];
}


- (void)adjustGraphWindowControls
{	
	id	obj;
	
	BOOL enabled = [[currentSettings objectForKey:SHOW_GRAPH_WINDOW_KEY] boolValue];
	NSColor *color = enabled ? [NSColor controlTextColor] : [NSColor controlHighlightColor];
	NSEnumerator *enumerator = [[graphWindowOptionsView subviews] objectEnumerator];
	
	while ((obj = [enumerator nextObject])) {
		if ([obj isMemberOfClass:[NSTextField class]])
			[obj setTextColor:color];
		else
			[obj setEnabled:enabled];
	}
}


- (IBAction)showPreferences:(id)sender
{
	double	freq;
	
	if (! panel) {
		if ([NSBundle loadNibNamed:@"Preferences" owner:self])
			[panel center];
		else {
			NSLog (@"Failed to load Preferences.nib");
			return;
		}
	}
	[wiredColor setColor:[currentSettings objectForKey:WIRED_COLOR_KEY]];
//	[activeColor setColor:[currentSettings objectForKey:ACTIVE_COLOR_KEY]];
//	[inactiveColor setColor:[currentSettings objectForKey:INACTIVE_COLOR_KEY]];
	[freeColor setColor:[currentSettings objectForKey:FREE_COLOR_KEY]];
	[pageinColor setColor:[currentSettings objectForKey:PAGEIN_COLOR_KEY]];
	[pageoutColor setColor:[currentSettings objectForKey:PAGEOUT_COLOR_KEY]];
	[transparency setFloatValue:[[currentSettings objectForKey:TRANSPARENCY_KEY] floatValue]];
	freq = [[currentSettings objectForKey:UPDATE_FREQUENCY_KEY] floatValue];
	[self showUpdateFrequency:(int)freq];
	[updateFrequencySlider setFloatValue:1000.0 * log(freq)];
	[pagingScale selectItemAtIndex:[pagingScale
		indexOfItemWithTag:[[currentSettings objectForKey:PAGING_SCALE_MAX_KEY] intValue]]];
	[pageinAtopPageout selectCellWithTag:[[currentSettings objectForKey:PAGEIN_ATOP_PAGEOUT_KEY] intValue]];
	[showPagingRate setState:[[currentSettings objectForKey:SHOW_PAGING_RATE_KEY] boolValue]];
	[showGraphWindow setState:[[currentSettings objectForKey:SHOW_GRAPH_WINDOW_KEY] boolValue]];
	[graphWindowOnTop setState:[[currentSettings objectForKey:GRAPH_WINDOW_ON_TOP_KEY] boolValue]];
	[graphWindowSize setFloatValue:[[currentSettings objectForKey:GRAPH_WINDOW_SIZE_KEY] floatValue]];
	[self adjustGraphWindowControls];
	[panel makeKeyAndOrderFront:nil];
}


- (IBAction)revertToDefaults:(id)sender
{
	[currentSettings release];
	currentSettings = [Preferences defaultPreferences];
	[self showPreferences:sender];
	[[NSNotificationCenter defaultCenter] postNotificationName:PREFERENCES_CHANGED object:nil];
}


- (void)savePreferences
{
	id		obj;
	float		r, g, b, a;
	NSString	*key;
	
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSEnumerator *enumerator = [currentSettings keyEnumerator];

	while ((key = [enumerator nextObject])) {
		obj = [currentSettings objectForKey:key];
		if ([obj isKindOfClass:[NSColor class]]) {
			[[obj colorUsingColorSpaceName:@"NSCalibratedRGBColorSpace"]
				getRed:&r green:&g blue:&b alpha:&a];
			[obj release];
			obj = [NSString stringWithFormat:@"%f %f %f", r, g, b];
		}
		[defaults setObject:obj forKey:key];
	}
}


- (id)objectForKey:(id)key
{
	return ([currentSettings objectForKey:key]);
}


- (IBAction)preferencesChanged:(id)sender
{
	int	freq;
	
	[currentSettings setObject:[wiredColor color] forKey:WIRED_COLOR_KEY];
//	[currentSettings setObject:[activeColor color] forKey:ACTIVE_COLOR_KEY];
//	[currentSettings setObject:[inactiveColor color] forKey:INACTIVE_COLOR_KEY];
	[currentSettings setObject:[freeColor color] forKey:FREE_COLOR_KEY];
	[currentSettings setObject:[pageinColor color] forKey:PAGEIN_COLOR_KEY];
	[currentSettings setObject:[pageoutColor color] forKey:PAGEOUT_COLOR_KEY];
	[currentSettings setObject:[NSNumber numberWithFloat:[transparency floatValue]] forKey:TRANSPARENCY_KEY];
	freq = exp([updateFrequencySlider doubleValue] / 1000.0);	/* 1..600 == 0.1 sec. to 1 min. */
	[self showUpdateFrequency:freq];
	[currentSettings setObject:[NSNumber numberWithInt:freq] forKey:UPDATE_FREQUENCY_KEY];
	[currentSettings setObject:[NSNumber numberWithInt:[[pagingScale selectedItem] tag]] forKey:PAGING_SCALE_MAX_KEY];
	[currentSettings setObject:[NSNumber numberWithInt:[[pageinAtopPageout selectedCell] tag]] forKey:PAGEIN_ATOP_PAGEOUT_KEY];
	[currentSettings setObject:[NSNumber numberWithInt:[showPagingRate state]] forKey:SHOW_PAGING_RATE_KEY];
	[currentSettings setObject:[NSNumber numberWithInt:[showGraphWindow state]] forKey:SHOW_GRAPH_WINDOW_KEY];
	[currentSettings setObject:[NSNumber numberWithInt:[graphWindowOnTop state]] forKey:GRAPH_WINDOW_ON_TOP_KEY];
	[currentSettings setObject:[NSNumber numberWithInt:[graphWindowSize intValue]] forKey:GRAPH_WINDOW_SIZE_KEY];
	[self adjustGraphWindowControls];
	[[NSNotificationCenter defaultCenter] postNotificationName:PREFERENCES_CHANGED object:nil];
}


- (int)windowNumber
{
	return (panel ? [panel windowNumber] : 0);
}


@end
